<?php
// 本类由系统自动生成，仅供测试用途
namespace Api\Controller;
use Think\Controller;
class FeijiUserController extends PublicController {
	//添加乘机订单
	//**********************************************
	public function add_pingji(){	
		
		$type=(int)$_GET['type'];//1 创建拼机 2加入拼机
		$uid=$_GET['uid']?$_GET['uid']:$_POST['uid'];//乘客id
		$start_place_id=$_GET['start_place_id']?$_GET['start_place_id']:$_POST['start_place_id'];//出发地
		$end_place_id=$_GET['end_place_id']?$_GET['end_place_id']:$_POST['end_place_id'];//目的地
		$start_time=$_GET['start_time']?$_GET['start_time']:$_POST['start_time'];//出发时间
		$end_time=$_GET['end_time']?$_GET['end_time']:$_POST['end_time'];//到达时间

		$start_airport=$_GET['start_airport']?$_GET['start_airport']:$_POST['start_airport'];//出发机场
		$end_airport=$_GET['end_airport']?$_GET['end_airport']:$_POST['end_airport'];//到达机场
		
		
		$money = $_GET['money']?$_GET['money']:$_POST['money'];//金额
		$seat=$_GET['seat']?$_GET['seat']:$_POST['seat'];//座位
		$mudi=$_GET['mudi']?$_GET['mudi']:$_POST['mudi'];//目的类型 1商务 2旅行 3医疗 4度假
		$feiji_type=$_GET['feiji_type']?$_GET['feiji_type']:$_POST['feiji_type'];//飞机型号
		$number = $_GET['number']?$_GET['number']:$_POST['number'];//人数
		$fei_time = $_GET['fei_time']?$_GET['fei_time']:$_POST['fei_time'];//飞行时间
		$pay_type = $_GET['pay_type']?$_GET['pay_type']:$_POST['pay_type'];//支付类型
		$username = $_GET['username']?$_GET['username']:$_POST['username'];//联系人
		$phone = $_GET['phone']?$_GET['phone']:$_POST['phone'];//联系人电话
		$isfap = $_GET['isfap']?$_GET['isfap']:$_POST['isfap'];//是否打印发票 1是 0否 默认0
		$invoice_id = $_POST['invoice_id'];//发票ID
		$address_id = $_POST['address_id'];//收货地址id
		//创建拼机
		if($type == 1){


			$array = array(
				'start_place' => $start_place_id,
				'end_place' => $end_place_id,
				'start_time' => strtotime($start_time),
				'addtime' => time(),				
				'mudi' => $mudi,
				'start_airport' => $start_place_id,
				'end_airport' => $end_place_id,
				'uid' => $uid,
			);

			$res = M('ping_ji')->add($array);
		}elseif($type == 2){//加入拼机
			$array = array(
				'pingid' => $_POST['pingid'],
				'start_place' => $start_place_id,
				'end_place' => $end_place_id,
				'start_time' => strtotime($start_time),
				'end_time' => strtotime($end_time),
				'addtime' => time(),
				'start_airport' => $start_airport,
				'end_airport' => $end_airport,
				'money' => $money,
				'seat' => $seat,
				'feiji_type' => $feiji_type,
				'number' => $number,
				'fei_time' => $fei_time,
				'type' => $pay_type,
				'username' => $username,
				'phone' => $phone,
				'uid' => $uid,
				'invoice_id' => $invoice_id,
				'address_id' => $address_id,
			);
			$res = M('ping_ji_order')->add($array);
		}	// print_r($array);die;
			
			
			  
			
			if ($res) {
				echo json_encode(array('status'=>1,'err'=>'添加成功'));
				exit();
			}else{
				echo json_encode(array('status'=>0,'err'=>'网络错误..'));
				exit();
			}
			
		
		
		

	}
	//首页类别
	public function index(){
		$data = M('feiji_cate')->where()->select();
		// print_r($data);die;

		echo json_encode(array('data' =>$data, 'status'=>1));exit();
	}
	//轮播图
	public function adlist(){
		$where = ' position = 1 ';
		$adlist = M('guanggao')->where()->order('sort desc')->select();
		echo json_encode(array('data'=>$adlist,'status'=>0));
	}
	//拼机列表
	public function pinji_list(){
		$where="1=1 AND del<1 AND status = 1 ";
		
		$count=M('ping_ji')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		// $page_index=$this->page_index($count,$rows,$page);
		$productlist=M('ping_ji')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		$data = $this->airport();
		// print_r($data);die;
		foreach ($productlist as $key => &$value) {
			$value['count_time'] = $this->count_time($value['start_time']);
			$value['start_time'] = date('Y-m-d H:i:s',$value['start_time']);
			$vr['start_place'] = $value['start_place'];
			$vr['end_place'] = $value['end_place'];
			$value['start_place'] = $data[$vr['start_place']]['address'];
			$value['end_place'] = $data[$vr['end_place']]['address'];
			$value['start_airport'] = $data[$vr['start_place']]['airport_name'];
			$value['end_airport'] = $data[$vr['end_place']]['airport_name'];
		}
		// print_r($productlist);die;
		echo json_encode(array('data'=>$productlist,'status'=>0));
	}
	//拼机详情
	public function pinji_xq(){
		$id = $_REQUEST['id'];
		$where="1=1 AND del<1 AND status = 1 and id = ".$id." ";
		$productlist=M('ping_ji')->where($where)->find();
		if(empty($productlist)){
			echo json_encode(array('status'=> -1,'err'=>'网络错误..'));die;
		}
		//获取飞机详情
		$data = $this->feiji_count($productlist['feiji_type']);
		$productlist['feiji_id'] = $productlist['feiji_type'];
		$productlist['feiji_type'] = $data['name'];
		$productlist['start_time'] = date('Y-m-d H:i:s',$productlist['start_time']);
		$productlist['addtime'] = date('Y-m-d H:i:s',$productlist['addtime']);
		//剩余座位数
		$setcount = M('ping_ji_order')->where('pingid ='.$id)->sum('number');
		$productlist['ping_people'] = $setcount;
		$productlist['over_seat'] = $productlist['seat']-$setcount;
		//获取小程序logo和名称
		$program = $this->feiji_max();
		$productlist['logo'] = $program['logo'];
		$productlist['title'] = $program['title'];
		//拼机推荐
		$guanggao=M('guanggao')->where('position = 2')->find();
		$productlist['tuijian'] = $guanggao;
		echo json_encode(array('data'=>$productlist,'status'=>0));die;
	}
	//包机列表
	public function baoji_list(){
		$where="1=1 AND del<1 AND status = 1 ";
		
		$count=M('bao_ji')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		// $page_index=$this->page_index($count,$rows,$page);
		$productlist=M('bao_ji')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		
		foreach ($productlist as $key => &$value) {
			$value['start_time'] = date('Y-m-d H:i:s',$value['start_time']);
			$data = $this->feiji_count($value['feiji_type']);
			$value['feiji_help'] = $data['name'];
		}
		echo json_encode(array('data'=>$productlist,'status'=>0));
	}
	//包机详情
	public function baoji_xq(){
		$id = $_REQUEST['id'];
		$where="1=1 AND del<1 AND status = 1 and id = ".$id." ";
		$baolist=M('bao_ji')->where($where)->find();
		if(empty($baolist)){
			echo json_encode(array('status'=> -1,'err'=>'网络错误..'));die;
		}
		//获取飞机详情
		$data = $this->feiji_count($baolist['feiji_type']);
		$productlist['feiji_id'] = $productlist['feiji_type'];
		$baolist['feiji_type'] = $data['name'];
		$baolist['start_time'] = date('Y-m-d H:i:s',$baolist['start_time']);
		$baolist['end_time'] = date('Y-m-d H:i:s',$baolist['end_time']);
		if($baolist['usertype'] ==1){
			$baolist['usertype'] = '单程';
		}elseif($baolist['usertype'] ==2){
			$baolist['usertype'] = '往返';
		}elseif($baolist['usertype'] ==3){
			$baolist['usertype'] = '多程';
		}
		if($baolist['isfood'] ==0){
			$baolist['isfood'] = '无餐食';
		}elseif($baolist['isfood'] ==1){
			$baolist['isfood'] = '中餐';
		}elseif($baolist['isfood'] ==2){
			$baolist['isfood'] = '西餐';
		}
		if($baolist['feiji_help'] ==0){
			$baolist['feiji_help'] = '无接机';
		}elseif($baolist['feiji_help'] ==1){
			$baolist['feiji_help'] = '接机';
		}elseif($baolist['feiji_help'] ==2){
			$baolist['feiji_help'] = '送机';
		}elseif($baolist['feiji_help'] ==3){
			$baolist['feiji_help'] = '接送机';
		}
		//剩余座位数
		// $setcount = M('bao_ji_order')->where('baoid ='.$id)->sum('number');
		// $baolist['ping_people'] = $setcount;
		// $baolist['over_seat'] = $baolist['seat']-$setcount;
		echo json_encode(array('data'=>$baolist,'status'=>0));
	}
	//包机
	public function add_baoji(){	
		
		$type=(int)$_GET['type'];//1创建包机 2预定包机
		$is_dancheng = $_GET['is_dancheng']?$_GET['is_dancheng']:1;//是否单程，1是  2往返  3多程
		$uid=$_GET['user_id']?$_GET['user_id']:$_POST['user_id'];//乘客id
		$start_place=$_GET['start_place']?$_GET['start_place']:$_POST['start_place'];//出发地
		$end_place=$_GET['end_place']?$_GET['end_place']:$_POST['end_place'];//目的地
		$start_time=$_GET['start_time']?$_GET['start_time']:$_POST['start_time'];//出发时间
		$end_time=$_GET['end_time']?$_GET['end_time']:$_POST['end_time'];//返程时间
		$baoji_id = $_POST['baoji'];//包机id
		$start_airport=$_GET['start_airport']?$_GET['start_airport']:$_POST['start_airport'];//出发航站楼
		$end_airport=$_GET['end_airport']?$_GET['end_airport']:$_POST['end_airport'];//到达航站楼
		$deposit=$_GET['deposit']?$_GET['deposit']:$_POST['deposit'];//定金
		$preferential_price = $_GET['preferential_price']?$_GET['preferential_price']:$_POST['preferential_price'];//优惠价
		$original_price=$_GET['original_price']?$_GET['original_price']:$_POST['original_price'];//原价
		$money = $_GET['money']?$_GET['money']:$_POST['money'];//金额
		$seat=$_GET['seat']?$_GET['seat']:$_POST['seat'];//座位
		$isfood=$_GET['isfood']?$_GET['isfood']:$_POST['isfood'];//是否餐饮 0 否 1中餐 2西餐
		$feiji_help=$_GET['feiji_help']?$_GET['feiji_help']:$_POST['feiji_help'];//是否接机 0否 1接 2送 3接送
		$feiji_type=$_GET['feiji_type']?$_GET['feiji_type']:$_POST['feiji_type'];//飞机型号
		$number = $_GET['number']?$_GET['number']:$_POST['number'];//人数
		$fei_time = $_GET['fei_time']?$_GET['fei_time']:$_POST['fei_time'];//飞行时间
		$pay_type = $_GET['pay_type']?$_GET['pay_type']:$_POST['pay_type'];//支付类型
		$username = $_GET['username']?$_GET['username']:$_POST['username'];//联系人
		$phone = $_GET['phone']?$_GET['phone']:$_POST['phone'];//联系人电话
		$invoice_id = $_POST['invoice_id'];//发票ID
		$address_id = $_POST['address_id'];//收货地址id
		//创建包机
		// if($type == 1){

			$array = array(
				'start_place' => $start_place,
				'end_place' => $end_place,
				'addtime' => time(),
				'number' => $number,
				'isfood' => $isfood,
				'feiji_help' => $feiji_help,
				'username' => $username,
				'phone' => $phone,
				'userType' => $is_dancheng,
				'start_airport' => $start_place,
				'end_airport' => $end_place,
				'invoice_id' => $invoice_id,
				'address_id' => $address_id,
				'uid' => $uid,
			);
			// print_r($array);die;
			if($is_dancheng == 1){//单程
				$array['start_time'] = strtotime($start_time);
				$res = M('bao_ji')->add($array);
				// echo M('bao_ji')->getLastsql();die;
			}elseif($is_dancheng == 2){//往返
				$array['start_time'] = strtotime($start_time);
				$array['end_time'] = strtotime($end_time);
				$res = M('bao_ji')->add($array);
			}elseif($is_dancheng == 3){//多程
				$startTime = explode(',', $start_time);
				$feiji_max = $this->feiji_max();
				if($feiji_max['fieji_max'] < count($startTime)){
					echo json_encode(array('status'=>-1,'err'=>'超过最大程次'));exit();
				}else{
					for ($i=0; $i<count($startTime); $i++) {
						$array['start_time'] = strtotime($startTime[$i]);
						$res = M('bao_ji')->add($array);
					}
				}
				
				
			}
		// }elseif($type == 2){//预定包机
		// 	$array = array(
		// 		'start_place' => $start_place,
		// 		'end_place' => $end_place,
		// 		'baoid' => $_POST['baoid'],//包机id
		// 		'start_time' => strtotime($start_time),
		// 		'end_time' => strtotime($end_time),
		// 		'addtime' => time(),
		// 		'start_airport' => $start_airport,
		// 		'end_airport' => $end_airport,
		// 		'money' => $money,//金额
		// 		'seat' => $seat,
		// 		'deposit' => $deposit,//定金
		// 		'preferential_price' => $preferential_price,//优惠价
		// 		'original_price' => $original_price,//原价
		// 		'feiji_type' => $feiji_type,
		// 		'number' => $number,
		// 		'fei_time' => $fei_time,
		// 		'type' => $pay_type,
		// 		'username' => $username,
		// 		'phone' => $phone,
		// 		'invoice_id' => $invoice_id,
		// 		'address_id' => $address_id,
		// 		'uid' => $uid,
		// 	);
		// 	$res = M('bao_ji_order')->add($array);
		// }	
			
		if ($res) {
			echo json_encode(array('status'=>1,'err'=>'添加成功'));
			exit();
		}else{
			echo json_encode(array('status'=>0,'err'=>'网络错误..'));
			exit();
		}	  
					

	}
	//添加乘客信息
	public function adduser(){
		$userid = $_POST['userid'];
		$name = $_GET['name']?$_GET['name']:$_POST['name'];//名字
		$six = $_GET['six']?$_GET['six']:$_POST['six'];//性别
		$birth_date = $_GET['birth_date']?$_GET['birth_date']:$_POST['birth_date'];//出生日期
		$english_x = $_GET['english_x']?$_GET['english_x']:$_POST['english_x'];//英文姓
		$english_m = $_GET['english_m']?$_GET['english_m']:$_POST['english_m'];//英文名
		$paper = $_GET['paper']?$_GET['paper']:$_POST['paper'];//证件
		$phone = $_GET['phone']?$_GET['phone']:$_POST['phone'];//电话
		$is_my = $_GET['is_my']?$_GET['is_my']:$_POST['is_my'];//是否本人 1是 0否  默认0
		$data = array(
			'userid' => $userid,
			'name' => $name,
			'six' => $six,
			'birth_date' => $birth_date,
			'english_x' => $english_x,
			'english_m' => $english_m,
			'paper' => $paper,
			'phone' => $phone,
			'is_my' => $is_my,
		);
		$res = M('feiji_user')->add($data);
		if ($res) {
			echo json_encode(array('status'=>1,'err'=>'添加成功'));
			exit();
		}else{
			echo json_encode(array('status'=>0,'err'=>'网络错误..'));
			exit();
		}
	}
	//我的乘客记录列表
	public function my_feiji_user(){
		$userid = $_POST['userid'];
		$result = M('feiji_user')->where('userid='.intval($userid))->select();
		echo json_encode($result);
	}
	//购机留言
	public function buy_message(){
		$userid = $_POST['userid'];
		$content = $_POST['content'];
		$feiji_type = $_POST['feiji_type'];
		$data = array(
			'userid' => $userid,
			'content' => $content,
			'feiji_type' => $feiji_type,
		);
		$res = M('buy_message')->add($data);
		if ($res) {
			echo json_encode(array('status'=>1,'err'=>'添加成功'));
			exit();
		}else{
			echo json_encode(array('status'=>0,'err'=>'网络错误..'));
			exit();
		}
	}
	//添加发票 
	public function add_invoice(){
		$title = $_POST['title'];//抬头
		$number = $_POST['number'];//税号
		$userid = $_POST['openid'];
		$data = array(
			'title' => $title,
			'number' => $number,
			'userid' => $userid,
			'addtime' => time(),
		);
		$result = M('invoice')->add($data);
		if ($result) {
			echo json_encode(array('status'=>1,'err'=>'添加成功'));
			exit();
		}else{
			echo json_encode(array('status'=>0,'err'=>'网络错误..'));
			exit();
		}
	}
	//我的发票列表
	public function my_invoice(){
		$userid = $_POST['openid'];
		$result = M('invoice')->where('userid='.intval($userid))->select();
		echo json_encode(array('data'=>$result,'status'=>0));
	}
	//发票信息
	public function invoice_xq(){
		$invoice_id = $_POST['invoice_id'];
		$result = M('invoice')->where('id ='.$invoice_id)->find();
		echo json_encode(array('data'=>$result,'err'=>'发票信息'));
	}
	//添加收货地址
	public function add_address(){
		$userid = $_POST['openid'];
		$username = $_POST['username'];
		$phone = $_POST['phone'];
		$diqu = $_POST['diqu'];
		$addres_xq = $_POST['addres_xq'];
		$cord = $_POST['cord'];
		$data = array(
			'userid' => $userid,
			'username' => $username,
			'phone' => $phone,
			'diqu' => $diqu,
			'addres_xq' => $addres_xq,
			'cord' => $cord,
			'addtime' => time(),
		);
		$result = M('user_address')->add($data);
		if ($result) {
			echo json_encode(array('status'=>1,'err'=>'添加成功'));
			exit();
		}else{
			echo json_encode(array('status'=>0,'err'=>'网络错误..'));
			exit();
		}
	}
	//我的收货地址列表
	public function my_address(){
		$userid = $_POST['openid'];
		$result = M('user_address')->where('userid='.intval($userid))->select();
		echo json_encode(array('data'=>$result,'status'=>0));
	}
	//收货地址详情
	public function address_xq(){
		$address_id = $_POST['address_id'];
		$result = M('user_address')->where('id ='.$address_id)->find();
		echo json_encode(array('data'=>$result,'err'=>'收货地址详情'));
	}
	//飞机详情
	public function feiji_xq(){
		$feiji_id = $_GET['feiji_id'];
		$data = $this->feiji_count($feiji_id);
		echo json_encode(array('status'=>1,'data'=>$data));
	}
	public function feiji_count($feiji_id){
		// $feiji_id = $_GET['feiji_id'];
		$data = M('feiji_type')->where('id ='.$feiji_id)->find();
		preg_match_all ("/<img src=(.*) \/>/U", $data['content'], $pat_array);
		
		// 	$data['content'] = str_replace("<img","<image",$data['content']);
		// 	$data['content'] = str_replace('alt="" />','></image>',$data['content']);
		
		return $data;
		
	}
	//在售飞机
	public function buy_feiji_list(){
		$data = M('hot_feiji')->where('del<1')->select();
		echo json_encode(array('data'=>$data,'status'=>0));
	}
	//在售飞机详情
	public function buy_feiji_xq(){
		$id = $_POST['id'];
		$data = M('hot_feiji')->where('del<1 and id='.$id)->find();
		echo json_encode(array('data'=>$data,'status'=>0));
	}
	//机场信息
	public function airport(){
		$data = M('airport')->where('del <1')->select();
		foreach($data as $vr){
			$list[$vr['id']] = $vr;
		}
		return $list;
	}
	//地区列表
	public function address(){
		$type= $_REQUEST['type'];//1国内 2国外
		$data = M('airport')->where('type ='.$type)->select();
		// print_r($data);die;
		//首字母
		$pinyin = M('pingyin')->where()->select();
		foreach($pinyin as $key => $tr){
			
			$py[$tr['name']] = $tr['id'];

			
		}
		// print_r($py_data);die;
		foreach($data as $vr){
			$dqu[$vr['pingyin']][] = $vr;
		}
		// print_r($dqu);die;
		foreach($py as $key => &$ds){
			
			// $py[$ds['pingyin']] = $dqu[$ds['pingyin']] ;
			$ds = $dqu[$ds];
			if(empty($ds)){
				unset($py[$key]);
			}
		}	
		
		// print_r($py);die;
		echo json_encode(array('data'=>$py,'status'=>0));
	}
	//
	//===============  获取后台配置 ========================
	////获取最大乘机数
	public function feiji_max(){
		$data = M('program')->where()->find();
		return $data;
	}
	//============ 时间差  ===============
	public function count_time($dataTime){
		
		//今天与指定日期相差多少天
		$Date_1=time();
		$Date_2=$dataTime;
		// $d1=strtotime($Date_1);
		// $d2=strtotime($Date_2);
		$Days=abs(round(($Date_2-$Date_1)/3600));
		if($Days >=24){
			$hour = $Days/24;
			$seatTime = intval($hour).'天';
			
		}else{
			
			$seatTime = intval($Days).'小时';
		}

		return $seatTime;
	}
}